##############################################################################################
## Stauffer Public Perceptions of Women's Inclusion and Feelings of Political Efficacy
## Replication Code - Figures from Main Text
############################################################################################

#Load Packages 
library(ggplot2)
library(foreign)
library(readstata13)
library(gridExtra)

#Load Data
state.scatter<-read.dta13("../figure_1a.dta")
cong.density<-read.dta13("../figurre_1b.dta")
congress<-read.csv("../figure_2.csv")
stateee.prob<-read.csv("../figure_3.csv")
panel2<-read.csv("../figure_4.csv")

#############################################################
#### Figure 1a: Scatter Plot ####
#############################################################
head(state.scatter)

scatter<-ggplot(state.scatter, aes(x=state_guess, y=state)) +
  geom_point(size=4, shape=1)+
  geom_smooth(method = "lm", size = 1.5, color="grey45")+
  ylab("% Women in R's State Leg. \n")+
  xlab("\n Respondent Estimates")+
  ggtitle(" ")+
  theme_bw()
scatter<-scatter+theme_bw() + theme(panel.grid.major = element_blank(),
                                              panel.grid.minor = element_blank(), panel.border=element_blank())
scatter<-scatter+  theme(axis.text=element_text(size=17),
                                   axis.title=element_text(size=17, face="bold"),
                                   plot.title=element_text(size=19, face="bold"))+
scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))
scatter<-scatter+theme(legend.justification=c(.5,.85), legend.position=c(.05,.95), legend.text=element_text(size=14), 
                                 legend.title=element_text(size=16))
scatter<-scatter+theme(legend.key.width=unit(2,"cm"))
scatter<-scatter+theme(axis.line.x=element_line(color="black", size=.5), axis.line.y=element_line(color="black", size=.5))

scatter

#############################################################
#### Figure 1b: Density Plot ####
#############################################################
cong.density$r_estimate<-ifelse(cong.density$r_estimate>50, 50, cong.density$r_estimate)
table(cong.density$r_estimate)

density<-ggplot(cong.density, aes(r_estimate))+
  geom_density(adjust=1, size=1.5, alpha=.45)
density<-density+scale_fill_manual(values=c("white"))
#density<-density+scale_color_manual(values=c("grey39", "grey55"), name="Year", labels=c("2016", "2018"))
density<-density+labs(x="\n Estimate % Women in Congress", y=" \n \n Density \n",
                      title= )
density<-density+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))
density<-density + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                         panel.background = element_blank(), panel.border=element_blank(), axis.line = element_line(colour = "black"))
density<-density+theme(axis.text=element_text(size=17),
                       axis.title=element_text(size=17, face="bold"),
                       plot.title=element_text(size=19, face="bold"))

density

#Combine to make full figure 
test<-grid.arrange(scatter, density, ncol=2)

#############################################################
#### Figure 2: Efficacy and Estimates of Cong. Rep.      ####
#############################################################
congress.plot<-ggplot(congress, aes(pooledr_estimate, y=pooledmargin, ymin=pooledll, ymax=pooledul))+
  geom_line(size=2) +
  geom_ribbon(size=1.5, alpha=.4, colour=NA)+
  # scale_colour_manual(values=c("forestgreen", "darkorchid4"), name="Gender", labels=c("Female", "Male"))+
  #  scale_fill_manual(values=c("forestgreen", "darkorchid4"), name="Gender", labels=c("Female", "Male"))+
  # scale_colour_manual(values=c("gray35", "gray60"), name="Gender", labels=c("Female", "Male"))+
  #scale_fill_manual(values=c("gray35", "gray60"), name="Gender", labels=c("Female", "Male"))+
  #scale_linetype_manual(values=c("solid", "dotted"), name="Gender", labels=c("Female", "Male"))+
  ylab("Efficacy \n")+
  xlab("Estimate % Women \n ")+
  ggtitle(" ")+
  theme_bw()+
  xlab("")+
  theme(legend.justification=c(1,1), legend.position=c(.25, .25))
congress.plot<-congress.plot+  xlab(" \n Estimate % Women")
congress.plot<-congress.plot+theme_bw() + theme(panel.grid.major = element_blank(),
                                                panel.grid.minor = element_blank())
congress.plot<-congress.plot+  theme(axis.text=element_text(size=20),
                                     axis.title=element_text(size=20, face="bold"),
                                     plot.title=element_text(size=22, face="bold"))
congress.plot<-congress.plot+coord_cartesian(ylim=c(.17, .5))
congress.plot<-congress.plot+scale_y_continuous(breaks=c(.15, .2, .25, .3, .35, .4, .45, .5))
congress.plot<-congress.plot+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits=c(0,50))+
  theme(legend.justification=c(.5,.85), legend.position=c(.20,.95), legend.text=element_text(size=16), 
        legend.title=element_text(size=18))
congress<-congress + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                           panel.background = element_blank(), panel.border=element_blank(), axis.line = element_line(colour = "black"))
congress.plot<-congress.plot+theme(legend.key.width=unit(1.5,"cm"))
congress.plot<-congress.plot+guides(colour=guide_legend(override.aes=list(size=1)))+
  theme(panel.spacing = unit(1.5, "lines")) +
  theme(strip.text.x = element_text(size = 16))

congress.plot

#############################################################
#### Figure 3: Estimates and State Responsiveness        ####
#############################################################
labels <- c(a = "Not At All", b = "Not Very", c = "Moderately", d = "Very")
state.plot<-ggplot(state.prob, aes(estimate, y=prob, ))+facet_grid(.~outcome, labeller=labeller(outcome=labels))+
  geom_line(size=2) +
  geom_ribbon(size=1.5, alpha=.3, colour=NA, aes(ymin=lower, ymax=upper))+  # scale_colour_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
  #scale_fill_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
  ylab("Predicted Probability \n")+
  xlab("Estimate % Women \n ")+
  ggtitle(" ")+
  theme_bw()+
  xlab("")
state.plot<-state.plot+  xlab(" \n Estimate % Women")
state.plot<-state.plot+theme_bw() + theme(panel.grid.major = element_blank(),
                                          panel.grid.minor = element_blank())
state.plot<-state.plot+  theme(axis.text=element_text(size=20),
                               axis.title=element_text(size=20, face="bold"),
                               plot.title=element_text(size=22, face="bold"), panel.spacing = unit(0.75, "lines"))
state.plot<-state.plot+coord_cartesian(ylim=c(0, .6))
state.plot<-state.plot+scale_y_continuous(breaks=c(.1, .2, .3, .4, .5, .6))
state.plot<-state.plot+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits=c(0,50))+
  theme(legend.justification=c(.5,.95), legend.position=c(.075,.95), legend.text=element_text(size=12), 
        legend.title=element_text(size=14))
state.plot<-state.plot+theme(legend.key.width=unit(1.5,"cm"))
state.plot<-state.plot+theme(axis.line.x=element_line(color="black", size=.5), axis.line.y=element_line(color="black", size=.5))+
  theme(strip.text.x = element_text(size = 16))

state.plot


#############################################################
#### Figure 4: Panel
#############################################################
panel_plot<-ggplot(panel2, aes(correction, y=delta, ymin=delta_ll, ymax=delta_ul))+
  geom_line(size=1.5) +
  geom_ribbon(size=1.5, alpha=.3, colour=NA)+
  ylab("\n Change in Efficacy \n")+
  xlab("Correction \n")+
  ggtitle(" ")+
  theme_bw()
panel_plot<-panel_plot+theme_bw() + theme(panel.grid.major = element_blank(),
                                          panel.grid.minor = element_blank())
panel_plot<-panel_plot+  xlab(" \n Correction ")
panel_plot<-panel_plot+  theme(axis.text=element_text(size=17),
                               axis.title=element_text(size=17, face="bold"),
                               plot.title=element_text(size=19, face="bold"))
panel_plot<-panel_plot+scale_x_continuous(breaks=c(-25, -20,-15, -10, -5, 0, 5,10, 15, 20), limits=c(-25, 20))
panel_plot<-panel_plot+scale_y_continuous(breaks=c(-.09, -.06, -.03, 0, .03, .06,.09), limits=c(-.1, .09))+
  theme(panel.spacing = unit(1.5, "lines")) +
  theme(strip.text.x = element_text(size = 14, face="bold"))+
  theme(axis.text.x=element_text(angle=40, hjust=1))+
  geom_vline(xintercept = 0, color="gray60", linetype="dashed", size=2)
panel_plot


